package cn.fl.system.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysFlowManageVService;
import cn.fl.system.transform.SysFlowManageVVOTransform;
import cn.fl.system.vo.SysFlowManageVVO;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.condition.SysFlowManageVQueryCondition;
import fl.flow.facade.flow.dto.SysFlowManageVDTO;

/**
 * @Description: 流程配置管理版本 客户端服务层接口实现
 */
@Service
public class SysFlowManageVServiceImpl implements ISysFlowManageVService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SysFlowManageVServiceImpl.class);
	
	/**
	 * IFlowApiService 暴露服务
	 */
	@Autowired
	private IFlowApiService flowApiService;

	@Override
	public SysFlowManageVVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		//TODO 业务逻辑
		SysFlowManageVDTO dto = null;//this.自定义ApiService.selectById(id);
		return SysFlowManageVVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysFlowManageVVO entity) throws BaseException{
		if(null == entity)return null;
		//return this.sysFlowManageVApiService.add(SysFlowManageVVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		//return this.sysFlowManageVApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}

	@Override
	public boolean modify(SysFlowManageVVO entity) throws BaseException{
		if(null == entity)return false;
		//return this.sysFlowManageVApiService.modify(SysFlowManageVVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		SysFlowManageVQueryCondition qc = (SysFlowManageVQueryCondition)page.getCondition();
		if(CollectionUtils.isEmpty(qc.getFlowManageIdList())) {//流程管理ID必须存在
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程管理主键不能为空!");
		};
		if(StringUtils.isEmpty(qc.getSysCd())) {//系统代码必须存在
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "系统代码不能为空!");
		};
		return this.flowApiService.searchFlowManageVListPage(page);
	}

	@Override
	public boolean modifyFlowManageVIsValidBy(Long id, Long manageId, Integer isValid) throws BaseException {
		if(null == id || null == isValid){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改主键或是否有效值参数不完整!");
		}
		//如果是开启操作把流程管理下不包括本版本ID的数据都设置成禁用
		if(isValid.equals(BaseConstant.IS_YESNO_YES)){
			this.flowApiService.modifyFlowManageVIsValidByManageId(manageId, BaseConstant.IS_YESNO_NO, CurrentThreadContext.getCurrentUserId(), new Date(), CurrentThreadContext.getCurrentSysCd());
		}
		return this.flowApiService.modifyFlowManageVIsValidBy(id, isValid, CurrentThreadContext.getCurrentUserId(), new Date(), CurrentThreadContext.getCurrentSysCd());
	}
}
